<?php
declare (strict_types = 1);

namespace app\admin\controller\wx;
use think\facade\Log;
class Member extends WxBase
{
    protected function initialize()
    {
        parent::initialize();
        $this->model = new \app\auth\model\Member;
        $this->validate = new \app\auth\validate\Member;
        $this->order = 'id desc';
    }
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $where[] = ['status', '=', 'normal'];
        $whereRaw = null;
        // 自定义搜索
        $search = $this->request->param('search',null);
        if($search) {
            $search = json_decode($search, true);
            // Log::write($search);
            if(is_array($search) && sizeof($search) > 0) {
                ((array_key_exists('wx_subscribe', $search)) && $where[] = ['wx_subscribe','=', $search['wx_subscribe']]);
                ((array_key_exists('block', $search) && !empty($search['block'])) && $where[] = ['block','=', $search['block']]);
                ((array_key_exists('wx_nickname', $search) && !empty($search['wx_nickname'])) && $where[] = ['wx_nickname','like', '%'.$search['wx_nickname'].'%']);
                ((array_key_exists('wx_tags', $search) && !empty($search['wx_tags'])) && $whereRaw = 'JSON_CONTAINS( wx_tags, "'.$search['wx_tags'].'")' );
            }
        }
        // 排序字段
        $sort_by = $this->request->param('sort/s', $this->model->getPk());
        // 升序/降序
        $desc = $this->request->param('descending/s', 'true');
        // 分页容量
        $page_size = $this->request->param('page_size/d',$this->page_size);
        // tags
        if($whereRaw) {
            $result = $this->model->whereRaw($whereRaw)->where($where)->order($this->order)->order($desc === 'true' ? $sort_by.' desc' : $sort_by)->paginate($page_size);
        } else {
            $result = $this->model->where($where)->order($this->order)->order($desc === 'true' ? $sort_by.' desc' : $sort_by)->paginate($page_size);
        }return json($result);
    }
    // 设置用户备注
    public function  remark(){
        $openid = $this->request->param('openid',null);
        $remark = $this->request->param('remark',null);
        $res = $this->app->user->remark($openid, $remark); // 成功返回boolean
        if($res) {
            $this->model->where('wx_openid',$openid)->save(['remark'=>$remark]);
            return json('成功！');
        }else {
            return json('错误',500);
        }
    }
    public function block() {
        $openid = $this->request->param('openid', null); // 数组
        $this->app->user->block($openid);
        // 或者多个用户
        foreach ($openid as $key => $value) {
            # code...
            $this->model->where('wx_openid',$value)->save(['block'=>'yes']);
        }
        return json('成功！');
    }
    public function unblock() {
        $openid = $this->request->param('openid', null); // 数组
        $this->app->user->unblock($openid);
        // 或者多个用户
        foreach ($openid as $key => $value) {
            # code...
            $this->model->where('wx_openid',$value)->save(['block'=>'no']);
        }
        return json('成功！');
    }
    public function get_all_tags() {
        $res = $this->app->user_tag->list();
        return json($res);
    }
    public function add_tag(){
        $name = $this->request->param('name', null);
        $res = $this->app->user_tag->create($name);
        return json($res);
    }
    public function update_tag() {
        $id = $this->request->param('id', null);
        $name = $this->request->param('name', null);
        $res = $this->app->user_tag->update(intval($id), $name);
        return json($res);
    }
    public function del_tag() {
        $id = $this->request->param('id', null);
        $this->app->user_tag->delete(intval($id));
        return json('删除成功！');
    }
    public function set_member_tags() {
        $openid = $this->request->param('openid',null);
        $tags = $this->request->param('tags/a',null);
        if(empty($openid) || empty($tags)){
            return json('错误的参数',500);
        }

        $tt = array_map(function($tag){
            return intval($tag);
        },$tags);
        $newset = $this->request->param('newset/a',null);
        if ($newset !== null) {
            foreach ($newset as $key => $value) {
                # code...
                $this->app->user_tag->tagUsers([$openid], intval($value));
            }
        }
        $unset = $this->request->param('unset/a',null);
        if ($unset !== null) {
            foreach ($unset as $key => $value) {
                # code...
                $this->app->user_tag->untagUsers([$openid], intval($value));
            }
        }
        $this->model->where('wx_openid', $openid)->save(['wx_tags'=>$tt]);
        return json('修改成功！');
    }
    // public function get_user_tags() {
    //     $userTags = $app->user_tag->userTags($openId);
    // }
    private function set_user_tag($openIds, $tagId) {
        return $this->app->user_tag->tagUsers($openIds, $tagId); // openid数组
    }
    private function unset_user_tag($openIds, $tagId) {
        return $this->app->user_tag->untagUsers($openIds, $tagId);  // openid数组
    }
}
